<template>
  <div class='setting-container'>
    <el-card>
      <!-- 导航 -->
      <div slot="header">
        <Breadcrumb>个人设置</Breadcrumb>
      </div>
      <!-- 栅格 -->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form ref="form" :model="userInfo" label-width="120px" :rules="rules" >
            <el-form-item label="编号：">100</el-form-item>
            <el-form-item label="手机：">{{userInfo.mobile}}</el-form-item>
            <el-form-item label="媒体名称：" prop="name">
              <el-input v-model="userInfo.name" ></el-input>
            </el-form-item>
            <el-form-item label="媒体简介：" prop="intro">
              <el-input v-model="userInfo.intro" type="textarea" :rows="3"></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：" prop="email">
              <el-input v-model="userInfo.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="updateUser" type="primary">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="uploadPhoto"
            :show-file-list="false">
            <img v-if="userInfo.photo" :src="userInfo.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Settings',
  data () {
    return {
      // 用户信息
      userInfo: {
        name: 'test',
        intro: 'info',
        email: 'info@qq.com',
        photo: '',
        mobile: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入媒体名称', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        intro: [
          { required: true, message: '请输入媒体简介', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      },
      // 支持上传组件的数据
      imageUrl: null
    }
  },
  created () {
    this.loadUserInfo()
  },
  methods: {
    updateUser () {
      console.log(this.form)
      // 验证信息
      this.$refs.form.validate(result => {
        console.log(result)
        // 验证通过
        if (result) {
          this.doUpdateUser()
        }
      })
    },
    // 修改用户头像
    async uploadPhoto (obj) {
      console.log('photo....', obj)
      // 获取本次要上传的文件
      const { file } = obj
      // 发请求  获取头像信息
      // 这个接口本质是文件上传，所以传参时。不能用普通的对象格式4
      // 而应该使用formData
      const fd = new FormData()
      fd.append('photo', file)
      const result = await this.$http({
        method: 'PATCH',
        url: '/mp/v1_0/user/photo',
        data: fd
        // data: {
        //   photo: file
        // }
      })
      console.log(result)
      // 更新
      this.userInfo.photo = result.data.data.photo
      // 通过eventBus通过layout.vue组件去更新头像
      // 发布事件
      this.$eventBus.$emit('update_user_photo', result.data.data.photo)
    },
    // 修改用户信息
    async doUpdateUser () {
      // TODO  表单验证
      try {
        const result = await this.$http({
          method: 'PATCH',
          url: '/mp/v1_0/user/profile',
          data: {
            name: this.userInfo.name,
            intro: this.userInfo.intro,
            email: this.userInfo.email
          }
        })
        console.log('修改：', result)
        this.$message.success('修改成功')
        // 通过eventBus通过layout.vue组件去更新信息
        // 发布事件
        this.$eventBus.$emit('update_user_name', this.userInfo.name)
      } catch (err) {
        console.log(err)
        this.$message.error('修改失败')
      }
    },
    // 获取用户信息
    async loadUserInfo () {
      const result = await this.$http({
        method: 'GET',
        url: '/mp/v1_0/user/profile'
      })
      console.log('结果', result)
      this.userInfo.name = result.data.data.name
      this.userInfo.email = result.data.data.email
      this.userInfo.intro = result.data.data.intro
      this.userInfo.mobile = result.data.data.mobile
      this.userInfo.photo = result.data.data.photo
    }
  }
}
</script>
<style scoped lang='less'>
  .avatar{
    width: 178px;
    height: 178px;
    line-height: 178px;
  }
  .avatar-uploader {
      text-align: center;
  }
  .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
  }
  .avatar-uploader /deep/ .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
  }
.edit-photo{
  font-size: 12px;
  text-align: center;
}
</style>
